package com.k.db.dameng;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/*
CREATE SCHEMA "TESTLJW3";
SET SCHEMA "TESTLJW3";

CREATE TABLE "PERSON_INFO"
(
  "id" VARCHAR(50) NOT NULL,
  "person" VARCHAR(50),
  "age" INTEGER,
  PRIMARY KEY("id")
);

INSERT INTO "PERSON_INFO" VALUES ('13','zsf13',20);
INSERT INTO "PERSON_INFO" ("id","person","age")VALUES ('1','zsf1',18);
COMMIT;

select "id","person","age"
from "TESTLJW3"."PERSON_INFO";

 INSERT INTO dmhr.city(city_id,city_name,region_id) VALUES('BJ','北京',1);
 INSERT INTO "PERSON_INFO" ("id","person","age")VALUES ('1','zsf1',18);
 */
public class DamengDDL {
    /**
     大小写敏感查询
     SELECT CASE_SENSITIVE();
     **/

    private static StringBuilder getInsertSegment(String schema,String tableName){
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO").append(" ");
        if(schema!=null && schema.trim().length()>0){
            sb.append("\"").append(schema).append("\"").append(".").append("\"").append(tableName).append("\"").append(" ");
        }else{
            sb.append("\"").append(tableName).append("\"").append(" ");
        }
        return sb;
    }
    public static String genInsertField(String schema,String tableName,String[] fieldArray,Object[] valueArray){
        StringBuilder sb = getInsertSegment(schema,tableName);
        // fields
        String[] fieldQuoteArray = Arrays.stream(fieldArray).map(s -> "\"" + s + "\"").toArray(String[]::new);
        String fieldQuoteCommaString = String.join(",",fieldQuoteArray);
        sb.append("(").append(fieldQuoteCommaString).append(")").append(" ");

        // values
        String[] valueQuoteArray = Arrays.stream(valueArray).map(s -> {
            if(s instanceof String){
                return "'" + s + "'";
            }else{
                return s+"";
            }
        }).toArray(String[]::new);
        String valueQuoteCommaString = String.join(",",valueQuoteArray);
        sb.append("VALUES");
        sb.append("(");
        sb.append(valueQuoteCommaString);
        sb.append(")");
        return sb.toString();
    }
    public static String genInsertField(String schema, String tableName, List<String> fieldList, List<Object> valueList){
        String[] fieldArray = fieldList.toArray(new String[0]);
        Object[] valueArray = valueList.toArray(new Object[0]);
        return genInsertField(schema,tableName,fieldArray,valueArray);
    }

    public static void main(String[] args) {
        String schema = "testljw1";
        String tableName = "person_info";
        String[] fields = new String[]{
                "id","name","age"
        };
        Object[] values = new Object[]{"1","go1",23};

        String sql = genInsertField(schema,tableName,fields,values);
        System.out.println(sql);
    }


}
